package com.five.welcome.controller;

import com.five.welcome.result.Result;
import com.five.welcome.utils.FaceDetectUtil;
import com.five.welcome.utils.MinioUtils;
import com.five.welcome.utils.MultipartFileToBase64Util;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author Ywt
 */
@CrossOrigin
@RestController
@Slf4j
@Tag(name = "图片上传接口")
public class UploadController {
    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private MultipartFileToBase64Util multipartFileToBase64Util;

    @Autowired
    private FaceDetectUtil faceDetectUtil;

    //检测人脸
    @Operation(summary = "检测人脸")
    @PostMapping("/faceDetect")
    public Result faceDetect(MultipartFile file) throws IOException {
        //将图片转化为base64字符串
        String generateBase64 = multipartFileToBase64Util.generateBase64(file);
        //检测图片是否有人脸
        boolean detected = faceDetectUtil.Detect(generateBase64);
        if (detected) {
            //如果有面部信息就上传到Minio
            return Result.ok();
        } else {
            //没有面部信息!
            return Result.error("您的照片未检测到面部信息!!");
        }
    }
    //上传图片
    @Operation(summary = "上传")
    @PostMapping("/upload")
    public Result upload(MultipartFile file){
            String url = minioUtils.upload(file);
            log.info(url);
            return Result.ok(url);
    }
}
